package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ProRoute;
import com.ruoyi.system.domain.ProRouteProcess;
import com.ruoyi.system.domain.ProRouteProduct;
import com.ruoyi.system.mapper.ProRouteMapper;
import com.ruoyi.system.service.IProRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;


/**
 * 工艺路线Service业务层处理
 *
 * @author t382
 * @date 2025-05-09
 */
@Service
public class ProRouteServiceImpl implements IProRouteService
{
    @Autowired
    private ProRouteMapper proRouteMapper;

    /**
     * 查询工艺路线
     *
     * @param routeId 工艺路线主键
     * @return 工艺路线
     */
    @Override
    public ProRoute selectProRouteByRouteId(Long routeId)
    {
        return proRouteMapper.selectProRouteByRouteId(routeId);
    }

    /**
     * 查询工艺路线列表
     *
     * @param proRoute 工艺路线
     * @return 工艺路线
     */
    @Override
    public List<ProRoute> selectProRouteList(ProRoute proRoute)
    {
        return proRouteMapper.selectProRouteList(proRoute);
    }

    /**
     * 新增工艺路线
     *
     * @param proRoute 工艺路线
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProRoute(ProRoute proRoute)
    {
        // 生成供应商编码
        String prefix = "R"; // 默认
        String randomPart = String.format("%04d", new Random().nextInt(10000));

        String clientCode = prefix + randomPart;
        proRoute.setRouteCode(clientCode);
        proRoute.setCreateTime(DateUtils.getNowDate());
        int rows = proRouteMapper.insertProRoute(proRoute);
        insertProRouteProcess(proRoute);
        insertProRouteProduct(proRoute); // 新增产品关联
        return rows;
    }

    /**
     * 修改工艺路线
     *
     * @param proRoute 工艺路线
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProRoute(ProRoute proRoute)
    {
        proRoute.setUpdateTime(DateUtils.getNowDate());
        proRouteMapper.deleteProRouteProcessByRouteId(proRoute.getRouteId());
        proRouteMapper.deleteProRouteProductByRouteId(proRoute.getRouteId()); // 删除旧产品关联
        insertProRouteProcess(proRoute);
        insertProRouteProduct(proRoute); // 插入新产品关联
        return proRouteMapper.updateProRoute(proRoute);
    }

    /**
     * 批量删除工艺路线
     *
     * @param routeIds 需要删除的工艺路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProRouteByRouteIds(Long[] routeIds)
    {
        proRouteMapper.deleteProRouteProcessByRouteIds(routeIds);
        proRouteMapper.deleteProRouteProductByRouteIds(routeIds); // 删除产品关联
        return proRouteMapper.deleteProRouteByRouteIds(routeIds);
    }

    /**
     * 删除工艺路线信息
     *
     * @param routeId 工艺路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProRouteByRouteId(Long routeId)
    {
        proRouteMapper.deleteProRouteProcessByRouteId(routeId);
        return proRouteMapper.deleteProRouteByRouteId(routeId);
    }

    /**
     * 新增工艺组成信息
     *
     * @param proRoute 工艺路线对象
     */
    public void insertProRouteProcess(ProRoute proRoute)
    {
        List<ProRouteProcess> proRouteProcessList = proRoute.getProRouteProcessList();
        Long routeId = proRoute.getRouteId();
        if (StringUtils.isNotNull(proRouteProcessList))
        {
            List<ProRouteProcess> list = new ArrayList<ProRouteProcess>();
            for (ProRouteProcess proRouteProcess : proRouteProcessList)
            {
                proRouteProcess.setRouteId(routeId);
                list.add(proRouteProcess);
            }
            if (list.size() > 0)
            {
                proRouteMapper.batchProRouteProcess(list);
            }
        }
    }
    /** 新增产品关联 */
    private void insertProRouteProduct(ProRoute proRoute) {
        List<ProRouteProduct> productList = proRoute.getProRouteProductList();
        Long routeId = proRoute.getRouteId();
        if (StringUtils.isNotNull(productList)) {
            List<ProRouteProduct> list = new ArrayList<>();
            for (ProRouteProduct product : productList) {
                product.setRouteId(routeId);
                list.add(product);
            }
            if (!list.isEmpty()) {
                proRouteMapper.batchProRouteProduct(list);
            }
        }
    }
}
